<template>
  <div>
    <!-- Form -->
    <el-button type="text" @click="dialogFormVisible = true; operteType = 'save'">添加收货地址</el-button>
    
    <el-dialog title="收货地址表" :visible.sync="dialogFormVisible" @close='handleClose'>

      <el-form :model="receiving_address">
        <el-form-item label="收货ID" :label-width="formLabelWidth">
          <el-input v-model="receiving_address.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="收货地址" :label-width="formLabelWidth">
          <el-input v-model="receiving_address.receiving_address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="收件人" :label-width="formLabelWidth">
          <el-input v-model="receiving_address.receiving_person" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="receiving_address.mobile_phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="账号" :label-width="formLabelWidth">
          <el-input v-model="receiving_address.user_id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="默认收货地址" :label-width="formLabelWidth">
          <el-input v-model="receiving_address.is_default" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>

    <el-table :data="receiving_addresses" style="width: 100%">
      <el-table-column label="ID" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货地址" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.receiving_address }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收件人" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.receiving_person }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.mobile_phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账号" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.user_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="默认收货地址" min-width="100">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.is_default }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" fixed="right" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>\

    <el-pagination background layout="prev,pager,next" :total="total" :page-size="pageSize" :current-page="current"
      @current-change="loadReceiving_address">
    </el-pagination>

  </div>
</template>
<script>
//引用vue对象
export default {
  name: 'Receiving_addressView',
  //绑定数据
  data() {
    return {
      formLabelWidth: "120px",
      dialogFormVisible: false,
      receiving_addresses: [], //商品数组
      receiving_address: { id: '', receiving_address: '', receiving_person: '', mobile_phone: '', user_id: '', is_default: '' },
      operteType: '',
      total: 1,
      pageSize: 5,
      current: 0
    }
  },
  methods: {
    //加载商品
    loadReceiving_address(current) {
      this.current = current
      this.$http.get('/receiving_address?m=page&current=' + this.current + "&pageSize=" + this.pageSize).then(resp => {
        console.log(resp.data)
        if (resp.data.data) {
          this.receiving_addresses = resp.data.data.records
          this.total = resp.data.data.total
        }
      })
    },
    // loadReceiving_address(){
    //     this.$http.get('/receiving_address?m=findAll').then(resp => {
    //         console.log(resp.data)
    //         //绑定后端返回的数据
    //         if(resp.data.data){
    //             this.receiving_addresses = resp.data.data
    //         }
    //     })
    // },
    handleEdit(index, row) {
      this.operteType = 'updata';
      this.dialogFormVisible = true;
      this.receiving_address = JSON.parse(JSON.stringify(row));
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm('是否删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http.get('/receiving_address?m=delete&id=' + row.id).then(resp => {
            //绑定后端返回的数据
            if (resp.data.data == true) {
              this.loadReceiving_address(this.current)
              this.$message({
                type: 'success',
                message: '删除成功'
              });
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    handleSave() {
      if (this.operteType == 'save') {
        this.$http.post('/receiving_address?m=save', this.$qs.stringify(this.receiving_address))
          .then(resp => {
            console.log('save receiving_address', resp.data)
            if (resp.data.data == true) {
              console.log('save successfully')
              this.loadReceiving_address(this.current);
            }
          })
      } else {
        this.$http.post('/receiving_address?m=update', this.$qs.stringify(this.receiving_address))
          .then(resp => {
            console.log('updata receiving_address', resp.data)
            if (resp.data.data == true) {
              console.log('updata successfully')
              this.loadReceiving_address(this.current);
            }
          })
      }
      this.dialogFormVisible = false
    },
    handleClose() {
      this.product_type = { id: '', receiving_address: '', receiving_person: '', mobile_phone: '', user_id: '', is_default: '' },
        this.operteType = ''
    }
  },
  mounted() {
    this.loadReceiving_address(1)
  }
}
</script>